<template>
  <van-icon :class="{TextColor: value}" :name="value ? 'star' : 'star-o'" @click="collect" :loading="isbtnLoading"/>
</template>

<script>
import { collectArticle, cancelArticle } from '@/api/user'
export default {
  name: 'collectIndex',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data() {
    return {
      isbtnLoading: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async collect() {
      this.isbtnLoading = true
      try {
        if (this.value) {
          // 已收藏(要取消)
          await cancelArticle(this.artId)
          this.$toast.success('取消收藏成功')
        } else {
          // 未收藏(要收藏)
          await collectArticle(this.artId)
          this.$toast.success('收藏成功')
        }
        this.$emit('input', !this.value)
      } catch (err) {
        this.$toast.fail('操作失败，请重试')
      }
      this.isbtnLoading = false
    }
  }
}
</script>

<style scoped lang="less">
.TextColor {
  color:red
}
</style>
